package com.davidtorma.site.client.resolver.impl;

import com.davidtorma.site.GalleryItem;
import com.davidtorma.site.StringHelper;
import com.davidtorma.site.client.resolver.GalleryImageResolver;
import com.davidtorma.site.shared.Constants;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

public final class GalleryImageResolverImpl implements GalleryImageResolver {
	public static final String ITEM_CLASS = "galeryimg";
	
	public Widget resolve(GalleryItem item) {
		FlowPanel panel = new FlowPanel();
		Element div = panel.getElement();
		div.addClassName(ITEM_CLASS);
		div.addClassName(item.getMainCategory());
		if (!StringHelper.isBlank(item.getSubCategory())) {
			div.addClassName(item.getSubCategory());
		}
		
		Image img = new Image(Constants.ICON_PATH + item.getPath());
		img.addClickHandler(new GalleryImageClickHandler(Constants.IMAGE_PATH + item.getPath()));
		panel.add(img);
		
		return panel;
	}
	
	public GalleryImageResolverImpl() {
		super();
	}
	
	private static class GalleryImageClickHandler implements ClickHandler {
		private PopupPanel popup = new PopupPanel(true);
		private Image image;
		
		public GalleryImageClickHandler(String imagePath) {
			image = new Image(imagePath);
			popup.add(image);
		}
		
		@Override
		public void onClick(ClickEvent event) {
			popup.show();
		}
	}

}
